UPDATE 指令用于修改表中的现有记录。
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition
以下是“Northwind”数据库的“Customers”表中的示例:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 5021 | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 5023 | Mexico |
4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
以下 SQL 查询使用新联系人和新城市 (City) 更新第一个客户 (CustomerID = 1)。
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City = 'Frankfurt'
WHERE CustomerID = 1
“客户”表中的选择现在如下所示:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1 | Alfreds Futterkiste | Alfred Schmidt | Obere Str. 57 | Frankfurt | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitucion 2222 | Mexico D.F. | 05021 | Mexico |
3 | Antonio Moreno Taqueria | Antonio Moreno | Mataderos 2312 | Mexico D.F. | 05023 | Mexico |
4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbkop | Christina Berglund | Berguvsvagen 8 | Lulea | S-958 22 | Sweden |
WHERE 表达式确定将更新多少记录。
对于国家/地区为“Mexico”的所有记录,以下 SQL 查询会将 ContactName 更改为“Juan”:
UPDATE Customers
SET ContactName = 'Juan'
WHERE Country = 'Mexico'
“客户”表中的选择现在如下所示:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1 | Alfreds Futterkiste | Alfred Schmidt | Obere Str. 57 | Frankfurt | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Juan | Avda. de la Constitucion 2222 | Mexico D.F. | 05021 | Mexico |
3 | Antonio Moreno Taqueria | Juan | Mataderos 2312 | Mexico D.F. | 05023 | Mexico |
4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbkop | Christina Berglund | Berguvsvagen 8 | Lulea | S-958 22 | Sweden |
UPDATE Customers
SET ContactName = 'Juan'
“客户”表中的选择现在如下所示:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1 | Alfreds Futterkiste | Juan | Obere Str. 57 | Frankfurt | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Juan | Avda. de la Constitucion 2222 | Mexico D.F. | 05021 | Mexico |
3 | Antonio Moreno Taqueria | Juan | Mataderos 2312 | Mexico D.F. | 05023 | Mexico |
4 | Around the Horn | Juan | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbkop | Juan | Berguvsvagen 8 | Lulea | S-958 22 | Sweden |